package cn.gok;

import java.io.*;

class Test{
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //将对象写入文件();
        从文件读取对象();
    }

    private static void 从文件读取对象() throws IOException, ClassNotFoundException {
        //创建对象流读取的二进制文件
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("day5//1.dat"));
        //不断地读取文件，直到文件末尾
        try {
            while (true){
                Object object = objectInputStream.readObject();
                if (object instanceof Role){
                    ((Role) object).beatEnemy();
                }else if (object instanceof Map){
                    ((Map) object).showScene();
                }
                System.out.println(object);
            }
        }catch (EOFException e){//捕获异常信息
            System.out.println("文件读取结束");
        }
        objectInputStream.close();
    }

    private static void 将对象写入文件() throws IOException {
        //创建对象流写入的二进制文件
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("day5//1.dat"));
        //创建3个对象
        Role r1 = new Role("李逍遥", 50, 10);
        Role r2 = new Role("重楼", 30, 80);
        Role r3 = new Role("白月", 500, 100);
        Map map = new Map(10, 20, "牛马厂");
        //写入对象流
        objectOutputStream.writeObject(r1);
        objectOutputStream.writeObject(r2);
        objectOutputStream.writeObject(r3);
        objectOutputStream.writeObject(map);
        objectOutputStream.close();
        System.out.println("写入成功!");
    }
}
class Map implements Serializable{
    private int x;
    private int y;
    private String scene;//场景名
    public void showScene(){
        System.out.println("地图中出现"+this.scene+"的美丽风景！");
    }

    public Map(int x, int y, String scene) {
        this.x = x;
        this.y = y;
        this.scene = scene;
    }

    @Override
    public String toString() {
        return "Map{" +
                "x=" + x +
                ", y=" + y +
                ", scene='" + scene + '\'' +
                '}';
    }
}
public class Role implements Serializable {
    private String name;
    private int hp;
    private int level;

    public void beatEnemy(){
        System.out.println(this.name+"在和敌人作战！");
    }
    public Role(String name, int hp, int level) {
        this.name = name;
        this.hp = hp;
        this.level = level;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHp() {
        return hp;
    }

    public void setHp(int hp) {
        this.hp = hp;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    @Override
    public String toString() {
        return "Role{" +
                "name='" + name + '\'' +
                ", hp=" + hp +
                ", level='" + level + '\'' +
                '}';
    }
}
